﻿using System;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.SharePoint.Administration;

namespace CodeCounsel.SharePoint.NavigationService
{
    [Guid("50c2366b-24a5-4b9a-ad47-e6d6177c630c")]
    public class NavigationService
        : SPService
    {
        public NavigationApplication Application
        {
            get { return Applications.Cast<NavigationApplication>().First(); }
        }

        public NavigationService()
        {
        }

        public NavigationService(SPFarm farm)
            : base(String.Empty, farm)
        {
        }

        public override void Provision()
        {
            base.Provision();
            NavigationApplication application = GetChild<NavigationApplication>(String.Empty);
            if (application == null)
            {
                application = NavigationApplication.Create(this);
                application.Update();
                application.Provision();
            }
        }

        public override void Unprovision()
        {
            base.Unprovision();
            NavigationApplication application = GetChild<NavigationApplication>(String.Empty);
            if (application != null)
            {
                application.Unprovision();
                application.Delete();
            }
        }
    }
}
